package com.geek.controller;

import com.geek.bean.BaseRespVo;
import com.geek.bean.data.CartCheckoutData;
import com.geek.bean.data.CartIndexData;
import com.geek.bean.vo.CartAddVO;
import com.geek.bean.vo.CartCheckedVO;
import com.geek.bean.vo.CartUpdateVO;
import com.geek.constants.SystemConfig;
import com.geek.service.CartService;
import com.geek.service.SystemService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 小豪
 * @version 1.0
 * @description 购物车
 * @date 2024-06-17 16:43
 */
@RestController
@RequestMapping("/wx/cart")
public class WxCartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private SystemService systemService;

    @GetMapping("goodscount")
    public BaseRespVo goodsCount() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.ok(0);
        } else {
            Integer userId = (Integer) subject.getPrincipal();
            return BaseRespVo.ok(cartService.getGoodsCount(userId));
        }
    }

    @GetMapping("index")
    public BaseRespVo index() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        CartIndexData data = cartService.index(userId);
        return BaseRespVo.ok(data);
    }

    @PostMapping("add")
    public BaseRespVo add(@RequestBody CartAddVO cartAddVO) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        return cartService.add(userId, cartAddVO);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedVO checkedVO) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        CartIndexData data = cartService.checked(userId, checkedVO);
        return BaseRespVo.ok(data);
    }

    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody CartAddVO cartAddVO) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        Integer cartId = cartService.fastAdd(userId, cartAddVO.getGoodsId(),
                cartAddVO.getNumber(), cartAddVO.getProductId());
        return BaseRespVo.ok(cartId);
    }

    @GetMapping("checkout")
    public BaseRespVo checkout(@RequestParam("cartId") Integer cartId,
                               @RequestParam("addressId") Integer addressId,
                               @RequestParam("couponId") Integer couponId,
                               @RequestParam("userCouponId") Integer userCouponId,
                               @RequestParam("grouponRulesId") Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        CartCheckoutData data;
        Map<String, String> map = systemService.toMap();
        int min = Integer.parseInt(map.get(SystemConfig.MARKET_EXPRESS_FREIGHT_MIN));
        BigDecimal freightMIn = BigDecimal.valueOf(min);
        int value = Integer.parseInt(map.get(SystemConfig.MARKET_EXPRESS_FREIGHT_VALUE));
        BigDecimal freightValue = BigDecimal.valueOf(value);
        if (cartId == 0) {
            data = cartService.checkout(userId, cartId, addressId, couponId, userCouponId, grouponRulesId, freightMIn, freightValue);
        } else {
            data = cartService.cartCheckoutFastAdd(userId, cartId, addressId, couponId, userCouponId, grouponRulesId, freightMIn, freightValue);
        }
        return BaseRespVo.ok(data);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String, List<Integer>> map) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        List<Integer> productIds = map.get("productIds");
        cartService.deleteByUserId(userId, productIds);
        return BaseRespVo.ok(cartService.index(userId));
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateVO updateVO) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.unAuthc();
        }
        Integer userId = (Integer) subject.getPrincipal();
        cartService.update(userId, updateVO);
        return BaseRespVo.ok();
    }
}
